<?php 
session_start();
include("admin-header.php"); 
$err="";
if(isset($_SESSION['error'])){
	$err = $_SESSION['error'];
	$_SESSION['error']="";
}
?>
	
<form method="post" action="admin-home.php">


	  
<table width="380" cellpadding="15" bgcolor="#CCCCCC">

		  
		  <?php


				if( $_POST['option'] == "editCategory"){

					$details = explode("@@@@", $_POST['editCategory']);
					$id=$details[0];
					$categoryMaster = DAOFactory::getCategorymasterDAO()->load($id);
					$categoryMaster->categoryMasterId=$id;
					$categoryMaster->categoryName=$_POST['categoryName22'];
					$categoryMaster->categoryDescription=$_POST['categoryDescription22'];
					$categoryMaster->position=$_POST['categoryPosition22'];

					$exist = DAOFactory::getCategorymasterDAO()->queryByExistingPosition($_POST['categoryPosition22'],$id);
					
					if($exist==null){
					
						DAOFactory::getCategorymasterDAO()->update($categoryMaster);

						?>
						
							<tr>
							  <td colspan="2"><div align="center" class="styleLabel style15 style11  style17">Success </div></td>
							</tr>
							<tr>
							  <td class="style16" style="font-size:15px" colspan="2">

						<?php

							echo "Your changes have been saved successfully ";

					}
					else{
						?>
						
							<tr>
							  <td colspan="2"><div align="center" class="styleLabel style15 style11  style17">Failure </div></td>
							</tr>
							<tr>
							  <td class="style16" style="font-size:15px" colspan="2">

						<?php

							echo "Position is occupied by category ".$exist[0]->categoryName;
							echo "<br>Please select another position";

					}					
						

				}


				if( $_POST['option'] == "addCategory"){

					
					$categoryMaster = new Categorymaster();

					$categoryMaster->categoryName=$_POST['categoryName'];
					$categoryMaster->categoryDescription=$_POST['categoryDescription'];
					$categoryMaster->position=$_POST['categoryPosition'];
					$categoryMaster->markForDelete=0;
					$categoryMaster->url=$_POST['categoryUrl'];
					$exist = DAOFactory::getCategorymasterDAO()->queryByPosition($_POST['categoryPosition']);
					
					if($exist==null){
					
						$catId = DAOFactory::getCategorymasterDAO()->insert($categoryMaster);

						?>
						
							<tr>
							  <td colspan="2"><div align="center" class="styleLabel style15 style11  style17">Success </div></td>
							</tr>
							<tr>
							  <td class="style16" style="font-size:15px" colspan="2">

						<?php

							echo "Your category has been added successfully ";

					}
					else{
						?>
						
							<tr>
							  <td colspan="2"><div align="center" class="styleLabel style15 style11  style17">Failure </div></td>
							</tr>
							<tr>
							  <td class="style16" style="font-size:15px" colspan="2">

						<?php

							echo "Position is occupied by category ".$exist[0]->categoryName;

					}

				}


				if( $_POST['option'] == "deleteCategory"){

					$details = explode("@@@@", $_POST['select']);
					$id=$details[0];
					
					$existingQ = DAOFactory::getQuestionmasterDAO()->queryByCategoryMasterId($id);
					if(count($existingQ)>0){

				
						?>
						
						<tr>
						  <td colspan="2"><div align="center" class="styleLabel style15 style11  style17">Failure </div></td>
						</tr>
						<tr>
						  <td class="style16" style="font-size:15px" colspan="2">

							Category cannot be deleted.<br>This category has existing questions.

						
						<?php

					}else{
					$catId = DAOFactory::getCategorymasterDAO()->delete($id);
						
				
					?>
						
						<tr>
						  <td colspan="2"><div align="center" class="styleLabel style15 style11  style17">Success </div></td>
						</tr>
						<tr>
						  <td class="style16" style="font-size:15px" colspan="2">

							Category deleted successfully.

							
					<?php

					}
				}



		  ?>
		  <br><a href="<?php echo $base ?>/php/manage-category.php"><br>Click here</a> to go back to manage category page.
		  </td>
        </tr>

      </table>
</form>  



<?php include("admin-footer.php"); ?>
